﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.LIB;
using YJKRevitBase.WinForm;
using YJKRevitBase.XmlUtility;

namespace YJKInspectModel.ModelValidation.ClearHeightCheck
{
	public partial class DrawZoneForm : System.Windows.Forms.Form
	{
		public bool Chain
		{
			get
			{
				return this.chbChain.Checked;
			}
		}

        public DrawZoneForm(WindowHandle hWndRevit)
		{
			this.InitializeComponent();
			//base.Icon = Resource.YArchitechIcon;
			this.m_hWndRevit = hWndRevit;
			this.chbChain.CheckState = CheckState.Checked;
			this.SetChecked(DrawMode.Line);
			ApplicationClearHeight.thisApp.isRun = true;
			this.xmlHelper.LoadXml(this.path, "Root");
		}

		private void tsbDrawLine_Click(object sender, EventArgs e)
		{
			this.SetChecked(DrawMode.Line);
			this.KeyEsc();
		}

		private void tsbRect_Click(object sender, EventArgs e)
		{
			this.SetChecked(DrawMode.Rect);
			this.KeyEsc();
		}

		private void tsbArc3Pt_Click(object sender, EventArgs e)
		{
			this.SetChecked(DrawMode.Arc3Pt);
			this.KeyEsc();
		}

		private void tsbArcCenter_Click(object sender, EventArgs e)
		{
			this.SetChecked(DrawMode.ArcCenter);
			this.KeyEsc();
		}

		private void tsbDelete_Click(object sender, EventArgs e)
		{
			this.SetChecked(DrawMode.Delete);
			this.KeyEsc();
		}

		private void tsbFinish_Click(object sender, EventArgs e)
		{
			this.SetChecked(DrawMode.Finish);
			this.KeyEsc();
		}

		public DrawMode GetChecked()
		{
			if (CheckState.Checked == this.tsbLine.CheckState)
			{
				return DrawMode.Line;
			}
			if (CheckState.Checked == this.tsbRect.CheckState)
			{
				return DrawMode.Rect;
			}
			if (CheckState.Checked == this.tsbArc3Pt.CheckState)
			{
				return DrawMode.Arc3Pt;
			}
			if (CheckState.Checked == this.tsbArcCenter.CheckState)
			{
				return DrawMode.ArcCenter;
			}
			return DrawMode.Line;
		}

		public void SetChecked(DrawMode eMode)
		{
			this.m_clickMode = true;
			if (eMode == DrawMode.Line)
			{
				this.m_eDrawMode = DrawMode.Line;
				this.chbChain.Enabled = true;
				this.tsbLine.CheckState = CheckState.Checked;
				this.tsbRect.CheckState = CheckState.Unchecked;
				this.tsbArc3Pt.CheckState = CheckState.Unchecked;
				this.tsbArcCenter.CheckState = CheckState.Unchecked;
				this.tsbDelete.CheckState = CheckState.Unchecked;
				this.tsbFinish.CheckState = CheckState.Unchecked;
				return;
			}
			if (DrawMode.Rect == eMode)
			{
				this.m_eDrawMode = DrawMode.Rect;
				this.chbChain.Enabled = false;
				this.tsbLine.CheckState = CheckState.Unchecked;
				this.tsbRect.CheckState = CheckState.Checked;
				this.tsbArc3Pt.CheckState = CheckState.Unchecked;
				this.tsbArcCenter.CheckState = CheckState.Unchecked;
				this.tsbDelete.CheckState = CheckState.Unchecked;
				this.tsbFinish.CheckState = CheckState.Unchecked;
				return;
			}
			if (DrawMode.Arc3Pt == eMode)
			{
				this.m_eDrawMode = DrawMode.Arc3Pt;
				this.chbChain.Enabled = true;
				this.tsbLine.CheckState = CheckState.Unchecked;
				this.tsbRect.CheckState = CheckState.Unchecked;
				this.tsbArc3Pt.CheckState = CheckState.Checked;
				this.tsbArcCenter.CheckState = CheckState.Unchecked;
				this.tsbDelete.CheckState = CheckState.Unchecked;
				this.tsbFinish.CheckState = CheckState.Unchecked;
				return;
			}
			if (DrawMode.ArcCenter == eMode)
			{
				this.m_eDrawMode = DrawMode.ArcCenter;
				this.chbChain.Enabled = false;
				this.tsbLine.CheckState = CheckState.Unchecked;
				this.tsbRect.CheckState = CheckState.Unchecked;
				this.tsbArc3Pt.CheckState = CheckState.Unchecked;
				this.tsbArcCenter.CheckState = CheckState.Checked;
				this.tsbDelete.CheckState = CheckState.Unchecked;
				this.tsbFinish.CheckState = CheckState.Unchecked;
				return;
			}
			if (DrawMode.Delete == eMode)
			{
				this.m_eDrawMode = DrawMode.Delete;
				this.tsbLine.CheckState = CheckState.Unchecked;
				this.tsbRect.CheckState = CheckState.Unchecked;
				this.tsbArc3Pt.CheckState = CheckState.Unchecked;
				this.tsbArcCenter.CheckState = CheckState.Unchecked;
				this.tsbDelete.CheckState = CheckState.Checked;
				this.tsbFinish.CheckState = CheckState.Unchecked;
				return;
			}
			if (DrawMode.Finish == eMode)
			{
				this.m_eDrawMode = DrawMode.Finish;
				this.tsbFinish.CheckState = CheckState.Unchecked;
				this.SaveDlgData();
			}
		}

		private void SaveDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "CheckTypeSelectForm"));
			this.xmlHelper.SelectNode(Path.Combine("Root", "CheckTypeSelectForm", "Params"));
			this.xmlHelper.SetAttribute("X", base.Location.X.ToString());
			this.xmlHelper.SetAttribute("Y", base.Location.Y.ToString());
			this.xmlHelper.SaveXml("");
		}

		private void LoadDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "CheckTypeSelectForm", "Params"));
			string attribute = this.xmlHelper.GetAttribute("X");
			string attribute2 = this.xmlHelper.GetAttribute("Y");
			if (!string.IsNullOrEmpty(attribute) && !string.IsNullOrEmpty(attribute2))
			{
				base.Location = new System.Drawing.Point(Convert.ToInt32(attribute), Convert.ToInt32(attribute2));
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void KeyEsc()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			DrawZoneForm.SendMessage(this.m_hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			DrawZoneForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			DrawZoneForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void DrawZoneForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				DrawZoneForm.SendMessage(this.m_hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void tsbLine_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbArc_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbBack_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbFinish_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void DrawZoneForm_Load(object sender, EventArgs e)
		{
			this.LoadDlgData();
		}

		private void tsbRect_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void DrawZoneForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.KeyEsc();
			ApplicationClearHeight.thisApp.isRun = false;
		}

		public DrawMode m_eDrawMode;

		public bool m_clickMode;

        private WindowHandle m_hWndRevit;

		private YJKRevitBase.XmlUtility.XmlHelper xmlHelper = new YJKRevitBase.XmlUtility.XmlHelper();

		private string path = Path.Combine(Product.WorkingLocation, "CheckTypeSelectForm.xml");

		private const string Root = "Root";

		private const string Node = "CheckTypeSelectForm";

		private const string ChildNode = "Params";

		private const string LocationX = "X";

		private const string LocationY = "Y";
	}
}
